#include <TGS/GeoMath2.0/Angle.h>
#include <cmath>

namespace
{
	const double PI = 4.0 * atan(1.0);
}

namespace TGS{
namespace GeoMath{


Angle::Angle(void)
	: _radian(0.0)
{
}

Angle::Angle( double radian )
	: _radian(radian)
{

}


Angle::~Angle(void)
{
}

Angle Angle::fromRadian( double radian )
{
	Angle angle(radian);

	return angle;
}

Angle Angle::fromDegree( double degree )
{
	double radian = degree * PI / 180.0;
	return fromRadian(radian);
}

Angle Angle::fromDMS( int degree, int minute, double second )
{
	double d = degree + minute / 60.0 + second / 3600.0;
	return fromDegree(d);
}

double Angle::toRadian() const
{
	return _radian;
}

double Angle::toDegree() const
{
	return _radian * 180.0 / PI;
}

void Angle::toDMS( int& degree, int& minute, double& second ) const
{
	double d = toDegree();
	degree = (int)d;
	double ms = d - degree;
	minute = int(ms * 60);
	second = (ms * 60 - minute) * 60;
}

Angle Angle::operator+( const Angle& other ) const
{
	return Angle::fromRadian(_radian + other._radian);
}

Angle Angle::operator-( const Angle& other ) const
{
	return Angle::fromRadian(_radian - other._radian);
}

Angle Angle::operator/( double factor ) const
{
	return Angle::fromRadian(_radian / factor);
}

bool Angle::operator==(const Angle& other) const
{
	return _radian == other._radian;
}

TGS::GeoMath::Angle Angle::fromD_MS( double d_ms )
{
	int degree = (int)d_ms;
	int minute = int((d_ms - degree) * 60);
	double second = (d_ms - degree - minute / 60.0) * 3600;

	return Angle::fromDMS(degree, minute, second);
}

double Angle::toD_MS() const
{
	int degree, minute;
	double second;

	toDMS(degree, minute, second);

	return degree + minute / 60.0 + second / 3600;
}

}//GeoMath
}//TGS